{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [],
   "source": [
    "def str_insert(str_origin, pos, str_add):\n",
    "    str_list = list(str_origin)    # 字符串转list\n",
    "    str_list.insert(pos, str_add)  # 在指定位置插入字符串\n",
    "    str_out = ''.join(str_list)    # 空字符连接\n",
    "    return  str_out\n",
    "\n",
    "def is_operator(char):\n",
    "    if char in '+-*/':\n",
    "        return True\n",
    "    else:\n",
    "        return False\n",
    "\n",
    "def define_priority(operator, currOpe):\n",
    "    if operator in '*/':\n",
    "        return True\n",
    "    elif operator in '+-' and currOpe in '+-':\n",
    "        return True\n",
    "    else:\n",
    "        return False\n",
    "\n",
    "def pre_deal(func):\n",
    "    func = func.replace(\"[\", \"(\").replace('{', '(').replace('}', ')').replace(']', ')')\n",
    "    print(\"Replace spaces\", end=': ')        \n",
    "    print(func)\n",
    "    \n",
    "    for i in range(len(func)):\n",
    "        if (func[i] == '-' and i == 0) or (func[i] == '-' and i > 0 and func[i-1] == '('):\n",
    "            func = str_insert(func, i, '0')\n",
    "    print(\"Deal zero\", end=': ')\n",
    "    print(func)\n",
    "    \n",
    "    preStack = []\n",
    "    operator = []\n",
    "    for i in range(len(func)):\n",
    "        if func[i].isnumeric():\n",
    "            numStr = ''\n",
    "            while(func[i].isnumeric() and i < len(func)):\n",
    "                numStr = numStr + func[i]\n",
    "                i = i + 1\n",
    "            i = i - 1\n",
    "            preStack.append(numStr)\n",
    "        else: \n",
    "            if (len(operator) == 0 or func[i] == '('):\n",
    "                operator.append(func[i])\n",
    "            elif func[i] == ')':\n",
    "                while(operator[-1] != '('):\n",
    "                    preStack.append(operator[-1])\n",
    "                    operator.pop()\n",
    "                operator.pop()\n",
    "            else: # (is_operator(func[i])): #while((!op.empty()&&cmp_op_priority(op.top(), pre[i])))\n",
    "                if(len(operator)>0 and define_priority(operator[-1], func[i])):\n",
    "                    preStack.append(operator[-1])\n",
    "                    operator.pop()\n",
    "                operator.append(func[i])\n",
    "    while(len(operator) > 0):\n",
    "        preStack.append(operator[-1])\n",
    "        operator.pop()\n",
    "    print(\"Done:\", end=': ')\n",
    "    print(preStack)\n",
    "    return preStack\n",
    "\n",
    "def calculate(preStack):\n",
    "    num = []\n",
    "    for i in range(len(preStack)):\n",
    "        if preStack[i].isnumeric():\n",
    "            num.append(int(preStack[i]))\n",
    "        else:\n",
    "            b = num[-1]\n",
    "            num.pop()\n",
    "            a = num[-1]\n",
    "            num.pop()\n",
    "            if preStack[i] == '+':\n",
    "                num.append(a+b)\n",
    "            if preStack[i] == '-':\n",
    "                num.append(a-b)\n",
    "            if preStack[i] == '*':\n",
    "                num.append(a*b)\n",
    "            if preStack[i] == '/':\n",
    "                num.append(a/b)\n",
    "    print(num)\n",
    "        \n",
    "            \n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Replace spaces: -3-2*(1+2)\n",
      "Deal zero: 0-3-2*(1+2)\n",
      "Done:: ['0', '3', '-', '2', '1', '2', '+', '*', '-']\n",
      "[-9]\n"
     ]
    }
   ],
   "source": [
    "calculate(pre_deal('-3-2*(1+2)'))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "34"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Method Recursive\n",
    "\n",
    "def compute(func):\n",
    "    func = func.replace(\"[\", \"(\").replace('{', '(').replace('}', ')').replace(']', ')')\n",
    "    print(\"Replace spaces\", end=': ')        \n",
    "    print(func)\n",
    "    # No parenthess\n",
    "    container = []\n",
    "    num = 0\n",
    "    for i in range(len(func)):\n",
    "        if func[i].isnumeric():\n",
    "            temp = ''\n",
    "            while(func[i].isnumeric and i < len(func)):\n",
    "                temp = temp + func[i]\n",
    "            num = int(temp)\n",
    "        else:\n",
    "            if i == '-':\n",
    "                container.a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import time\n",
    "def save_data_pickle(data, filepath='', instType='radar', fileType='pickle'):\n",
    "    \"\"\"\n",
    "    \n",
    "    :param data:\n",
    "    :param filepath:\n",
    "    :param instType:\n",
    "    :param fileType:\n",
    "    :return:\n",
    "    \"\"\"\n",
    "    filename = time.strftime(\"%Y_%m_%d_%H_%M_%S\", time.localtime()) + '_' + instType\n",
    "    if fileType == 'pickle':\n",
    "        import pickle\n",
    "        filename = filename + '.pkl'\n",
    "    if fileType == 'csv':\n",
    "        import pandas as pd\n",
    "        filename = filename + '.csv'\n",
    "    if not filepath:\n",
    "        filepath = \"./data\"\n",
    "    filepath = filepath + '/' + filename\n",
    "    try:\n",
    "        if fileType == 'pickle':\n",
    "            f = open(filepath, 'wb')\n",
    "            pickle.dump(data, f)\n",
    "            f.close()\n",
    "            return f.name\n",
    "        if fileType == 'csv':\n",
    "            save = pd.DataFrame(data)\n",
    "            save.to_csv(filepath, index=False, header=False)\n",
    "            return filepath\n",
    "    except Exception as e:\n",
    "        return -1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'./2021_07_16_14_12_38_list.csv'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = [i for i in range(0, 10)]\n",
    "save_data_pickle(a, '.', 'list', 'csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
